home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / dirscan.com / LOCATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-07-20  |  2.9 KB  |  148 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos.h>
  4. #include <ctype.h>
  5. #include "dirscan.h"
  6.  
  7. char   path    [_MAX_DIR];
  8. char   drive   [_MAX_DRIVE];
  9. char   fn       [_MAX_FNAME];
  10. char   ext       [_MAX_EXT];
  11. char   file_id [_MAX_FNAME+_MAX_EXT-1];
  12. char   cur_path[_MAX_DIR];
  13. char   displayed;
  14.  
  15. long   num_files=0, num_dirs=0, num_found=0;
  16.  
  17. void display_dir(char[]);
  18. void display_file(char[],struct find_t);
  19.  
  20. main(argc,argv)
  21. int   argc;
  22. char *argv[];
  23. {
  24.  
  25.    char dpath[_MAX_PATH];
  26.    int rc;
  27.  
  28.    if (argc < 2)
  29.    {
  30.       fprintf(stderr,"No filename specified\n");
  31.       exit(1);
  32.    }
  33.  
  34.    /* parse the first argument */
  35.  
  36.    _splitpath(argv[1],drive,path,fn,ext);
  37.  
  38.    if (drive[0] == '\0')  /* drive not specified */
  39.    {
  40.  
  41.       /* use the currently logged drive */
  42.  
  43.       unsigned dnum;
  44.       _dos_getdrive(&dnum);
  45.       drive[0] = * ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"+dnum-1);
  46.       drive[1] = ':';
  47.       drive[2] = '\0';
  48.  
  49.    }
  50.    else   /* drive specified */
  51.    {
  52.  
  53.       /* make sure it's valid */
  54.  
  55.       if (strlen(drive) != 2 || !isalpha(drive[0]) || drive[1] != ':')
  56.       {
  57.          fprintf(stderr,"Invalid drive specification\n");
  58.          exit (-1);
  59.       }
  60.  
  61.    }
  62.  
  63.    if (path[0] == '\0')     /* if path is null, set it to '\' */
  64.    {
  65.       path[0] = '\\';
  66.       path[1] = '\0';
  67.    }
  68.  
  69.    if (fn[0] == '\0')       /* if filename is null, set it to '*' */
  70.    {
  71.       fn[0] = '*';
  72.       fn[1] = '\0';
  73.    }
  74.  
  75.    if (ext[0] == '\0')       /* if extension is null, set it to '*' */
  76.    {
  77.       ext[0] = '.';
  78.       ext[1] = '*';
  79.       ext[2] = '\0';
  80.    }
  81.  
  82.    /* build current path and filename.ext strings */
  83.  
  84.    strcpy(dpath,drive);
  85.    strcat(dpath,path);
  86.    strupr(dpath);
  87.  
  88.    strcpy(file_id,fn);
  89.    strcat(file_id,ext);
  90.    strupr(file_id);
  91.  
  92.    /* clear screen and display initialization message */
  93.  
  94.    system("CLS");
  95.    printf("Searching for %s%s\n",dpath,file_id);
  96.  
  97.    /* invoke "dirscan" search routine */
  98.  
  99.    num_files = dirscan(dpath,             /* Initial Path        */
  100.                        file_id,          /* File Name Mask        */
  101.                        _A_NORMAL,         /* File Attribute Mask */
  102.                        display_dir,      /* Directory Routine    */
  103.                        display_file);     /* File Routine        */
  104.  
  105.    printf("\n%8ld files found",num_found);
  106.    printf("%8ld directories searched",num_dirs);
  107.    printf("%8ld files examined\n",num_files);
  108.  
  109.    exit(0);
  110. }
  111.  
  112.  
  113. void display_dir(path)
  114. char  path[];
  115. {
  116.  
  117.    strcpy(cur_path,path);
  118.    displayed = 0;
  119.    num_dirs++;
  120.  
  121. }
  122.  
  123. void display_file(path,buffer)
  124. char            path[];
  125. struct find_t    buffer;
  126. {
  127.  
  128.     FILE_TIME *time = (FILE_TIME *) &buffer.wr_time;
  129.     FILE_DATE *date = (FILE_DATE *) &buffer.wr_date;
  130.  
  131.     num_found++;
  132.  
  133.     if (!displayed)
  134.     {
  135.         printf("%s\n",cur_path);
  136.         displayed = 1;
  137.     }
  138.  
  139.     printf("   %-12s %9ld",buffer.name,buffer.size);
  140.     printf("    %2.2d:%2.2d:%2.2d",time->hour,
  141.                                    time->minute,
  142.                                    time->second*2);
  143.     printf("  %2.2d/%2.2d/%4.4d\n",date->month,
  144.                                    date->day,
  145.                                    date->year+1980);
  146.  
  147. }
  148.